/*
 * logic_utility.cpp
 *
 *  Created on: 2025年1月3日
 *      Author: Administrator
 */
#include "can/context.h"
#include "logic_utility.hpp"
#include <unordered_set>
#include <base/handler.hpp>

#define MESSAGE_GEAR 77
class GearController {
public:
	// 构造函数
	GearController() :
			GearFlag(1),GearFlag1(1), handler_(staticMessageHandler, this) {
	}

	// 回调函数，用于处理消息
	static void staticMessageHandler(const base::message<ZKTextView*>* msg,
			const void* user_data) {
		auto self = static_cast<GearController*>(const_cast<void*>(user_data));
		self->messageHandler(msg, user_data);
	}

	void messageHandler(const base::message<ZKTextView*>* msg,
			const void* user_data) {
		if (msg->what == MESSAGE_GEAR) {
			shining(msg->obj);
		}else if(msg->what == 78){
			shining1(msg->obj);
		}
	}

	void shining(ZKTextView* gearPtr) {
		switch (condition.gear) {
		case 2: {
			if (GearFlag) {
				GearFlag = 0;
				gearPtr->setBackgroundPic("statusbar/gear/P_1.png");
			} else {
				GearFlag = 1;
				gearPtr->setBackgroundPic("statusbar/gear/P_0.png");
			}
			break;
		}
		case 4: {
			if (GearFlag) {
				GearFlag = 0;
				gearPtr->setBackgroundPic("statusbar/gear/R_1.png");
			} else {
				GearFlag = 1;
				gearPtr->setBackgroundPic("statusbar/gear/R_0.png");
			}
			break;
		}
		case 6: {
			if (GearFlag) {
				GearFlag = 0;
				gearPtr->setBackgroundPic("statusbar/gear/N_1.png");
			} else {
				GearFlag = 1;
				gearPtr->setBackgroundPic("statusbar/gear/N_0.png");
			}
			break;
		}
		case 8: {
			char buf[48] = { };
			if (GearFlag) {
				GearFlag = 0;
				sprintf(buf, "statusbar/gear/D%d_1.png", condition.gearGrade);
				gearPtr->setBackgroundPic(buf);
			} else {
				GearFlag = 1;
				sprintf(buf, "statusbar/gear/D%d_0.png", condition.gearGrade);
				gearPtr->setBackgroundPic(buf);
			}
			break;
		}

		}
	}
	void shining1(ZKTextView* gearPtr) {
			switch (info0.gear) {
			case 2: {
				if (GearFlag1) {
					GearFlag1 = 0;
					gearPtr->setBackgroundPic("mode1/gear/P_1.png");
				} else {
					GearFlag1 = 1;
					gearPtr->setBackgroundPic("mode1/gear/P_0.png");
				}
				break;
			}
			case 4: {
				if (GearFlag1) {
					GearFlag1 = 0;
					gearPtr->setBackgroundPic("mode1/gear/R_1.png");
				} else {
					GearFlag1 = 1;
					gearPtr->setBackgroundPic("mode1/gear/R_0.png");
				}
				break;
			}
			case 6: {
				if (GearFlag1) {
					GearFlag1 = 0;
					gearPtr->setBackgroundPic("mode1/gear/N_1.png");
				} else {
					GearFlag1 = 1;
					gearPtr->setBackgroundPic("mode1/gear/N_0.png");
				}
				break;
			}
			case 8: {
				char buf[48] = { };
				if (GearFlag1) {
					GearFlag1 = 0;
					sprintf(buf, "mode1/gear/D%d_1.png", condition.gearGrade);
					gearPtr->setBackgroundPic(buf);
				} else {
					GearFlag1 = 1;
					sprintf(buf, "mode1/gear/D%d_0.png", condition.gearGrade);
					gearPtr->setBackgroundPic(buf);
				}
				break;
			}

			}
		}
	void startShining(ZKTextView* gearPtr, int add) {
		base::message<ZKTextView*> msg(add, gearPtr);
		handler_.schedule(msg, 500, 0);
	}
	void stopShining(int remove) {
		handler_.remove_messages(
				[](const base::message<ZKTextView*>& msg, void* user_data) -> bool {
					int* remove_ptr = static_cast<int*>(user_data);
					return msg.what == *remove_ptr;
				}, &remove);
	}
private:
	int GearFlag;
	int GearFlag1;
	// 定义 handler 成员变量
	base::handler<ZKTextView*> handler_;
};
static GearController gearCtrl;

static int pre_gear = 1;
void setGear(ZKTextView* gearPtr, ZKTextView* RankPtr, bool &isRegistered) {
	gearPtr->setVisible(true);
	if (condition.gear == 0) {
		gearPtr->setVisible(false);
	} else if (condition.gear == 2 || condition.gear == 4
			|| condition.gear == 6 || condition.gear == 8) {
#if 0
		switch(condition.gear){
			case 2:
			{
				gearPtr->setBackgroundPic("statusbar/gear/P_0.png");
				mshiningGearPtr->setBackgroundPic("statusbar/gear/P_1.png");
				break;
			}
			case 4:
			{
				gearPtr->setBackgroundPic("statusbar/gear/R_0.png");
				mshiningGearPtr->setBackgroundPic("statusbar/gear/R_1.png");
				break;
			}
			case 6:
			{
				gearPtr->setBackgroundPic("statusbar/gear/N_0.png");
				mshiningGearPtr->setBackgroundPic("statusbar/gear/N_1.png");
				break;
			}
			case 8:
			{
				char buf[48]={};
				char buff[48]={};
				sprintf(buff,"statusbar/gear/D%d_1.png",condition.gearGrade);
				mshiningGearPtr->setBackgroundPic(buff);

				sprintf(buf,"statusbar/gear/D%d_0.png",condition.gearGrade);
				gearPtr->setBackgroundPic(buf);
				break;
			}
		}
#endif
		if (!isRegistered) {
			gearCtrl.startShining(gearPtr, MESSAGE_GEAR);
			isRegistered = true;
		}
	} else {
		if (isRegistered) {
			gearCtrl.stopShining(MESSAGE_GEAR);
			isRegistered = false;
		}

		if (condition.gear == 0xFE) {
			switch (pre_gear) {
			case 1:
				gearPtr->setBackgroundPic("statusbar/gear/P_2.png");
				break;
			case 3:
				gearPtr->setBackgroundPic("statusbar/gear/R_2.png");
				break;
			case 5:
				gearPtr->setBackgroundPic("statusbar/gear/N_2.png");
				break;
			case 7:
				gearPtr->setBackgroundPic("statusbar/gear/D0_2.png");
				break;
			}
		} else {
			switch (condition.gear) {
			case 1:
				gearPtr->setBackgroundPic("statusbar/gear/P_0.png");
				pre_gear = 1;
				break;
			case 3:
				gearPtr->setBackgroundPic("statusbar/gear/R_0.png");
				pre_gear = 3;
				break;
			case 5:
				gearPtr->setBackgroundPic("statusbar/gear/N_0.png");
				pre_gear = 5;
				break;
			case 7: {
				pre_gear = 7;
				char buf[48] = { };
				sprintf(buf, "statusbar/gear/D%d_0.png", condition.gearGrade);
				gearPtr->setBackgroundPic(buf);
				break;
			}

			}
		}
	}
	if (condition.gearRank == 0) {
		RankPtr->setVisible(false);
	} else {
		RankPtr->setVisible(true);
		switch (condition.gearRank) {
		case 1:
			RankPtr->setText("-1+");
			break;
		case 2:
			RankPtr->setText("-2+");
			break;
		case 3:
			RankPtr->setText("-3+");
			break;
		case 4:
			RankPtr->setText("-4+");
			break;
		case 5:
			RankPtr->setText("-5+");
			break;
		case 6:
			RankPtr->setText("-6+");
			break;
		case 7:
			RankPtr->setText("-7+");
			break;
		case 8:
			RankPtr->setText("-8+");
			break;
		}
	}
}

void setGear1(ZKTextView* gearPtr, ZKTextView* RankPtr, bool &isRegistered) {
	gearPtr->setVisible(true);
	if (info0.gear == 0) {
		gearPtr->setVisible(false);
	} else if (info0.gear == 2 || info0.gear == 4
			|| info0.gear == 6 || info0.gear == 8) {
		if (!isRegistered) {
			gearCtrl.startShining(gearPtr, 78);
			isRegistered = true;
		}
	} else {
		if (isRegistered) {
			gearCtrl.stopShining(78);
			isRegistered = false;
		}

		if (info0.gear == 0xFE) {
			switch (pre_gear) {
			case 1:
				gearPtr->setBackgroundPic("mode1/gear/P_2.png");
				break;
			case 3:
				gearPtr->setBackgroundPic("mode1/gear/R_2.png");
				break;
			case 5:
				gearPtr->setBackgroundPic("mode1/gear/N_2.png");
				break;
			case 7:
				gearPtr->setBackgroundPic("mode1/gear/D0_2.png");
				break;
			}
		} else {
			switch (info0.gear) {
			case 1:
				gearPtr->setBackgroundPic("mode1/gear/P_0.png");
				pre_gear = 1;
				break;
			case 3:
				gearPtr->setBackgroundPic("mode1/gear/R_0.png");
				pre_gear = 3;
				break;
			case 5:
				gearPtr->setBackgroundPic("mode1/gear/N_0.png");
				pre_gear = 5;
				break;
			case 7: {
				pre_gear = 7;
				char buf[48] = { };
				sprintf(buf, "mode1/gear/D%d_0.png", info0.gearGrade);
				gearPtr->setBackgroundPic(buf);
				break;
			}

			}
		}
	}
	if (info0.gearRank == 0) {
		RankPtr->setVisible(false);
	} else {
		RankPtr->setVisible(true);
		switch (info0.gearRank) {
		case 1:
			RankPtr->setText("-1+");
			break;
		case 2:
			RankPtr->setText("-2+");
			break;
		case 3:
			RankPtr->setText("-3+");
			break;
		case 4:
			RankPtr->setText("-4+");
			break;
		case 5:
			RankPtr->setText("-5+");
			break;
		case 6:
			RankPtr->setText("-6+");
			break;
		case 7:
			RankPtr->setText("-7+");
			break;
		case 8:
			RankPtr->setText("-8+");
			break;
		}
	}
}

void setSpeed(ZKTextView* pData, const uint16_t& shortValue){
	pData->setText(shortValue);
}


Refreshing& Refreshing::getInstance() {
	static Refreshing sFreshing;
	return sFreshing;
}

bool Refreshing::refreshTach()
{
	if(tachEnd == tachStart){
		return false;
	}
	int sign = 1;
	sign = (tachEnd - tachStart) > 0 ? sign : -sign;

	tachStart += sign;

	PointerRpm->setTargetAngle(tachStart);
	return true;
}

bool Refreshing::refreshSpeed()
{
	if(speedEnd == speedStart){
		return false;
	}
	int sign = 1;
	sign = (speedEnd - speedStart) > 0 ? sign : -sign;

	speedStart += sign;

	PointerSpeed->setTargetAngle(speedStart);

	return true;
}
bool Refreshing::readyToRun(){
	flagTach = true;
	flagSpd = true;
	return true;
}

bool Refreshing::threadLoop() {
	if((flagTach || flagSpd))
	{
		flagTach = refreshTach();
		flagSpd = refreshSpeed();
	}else{
		return false;
	}

	usleep(1000 * 10); //50ms
	//返回真，继续下次线程循环
	return true;
}
